package com.naicheng.ivote.views.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.Answers;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.FindNewBean;
import com.naicheng.ivote.doman.FindResult;
import com.naicheng.ivote.doman.FindResultBean;
import com.naicheng.ivote.engin.BaseFragment;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.uiself.LinearLayoutForListView;
import com.naicheng.ivote.uiself.ToastUtils;
import com.naicheng.ivote.volley.Response.Listener;

public class FindResultFragment extends BaseFragment {
	// 测试区
	private ListView listView;
	private FindResultAdapter adapter;
	private TextView tv_count;
	@Override
	protected View initViews(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.find_result_fragment, null,false);
		return v;
	}

	
	
	String requestcommand = "quizrewardlist";
	protected void init() {
		tv_count = getView(R.id.tv_usercount);
		listView = getView(R.id.lv_result);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				FindNewBean item = adapter.getItem(position);
				// TODO 跳详情
			}
		});
			
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userid", ConstantMine.userid);
		getDataFromServer(requestcommand , map , new Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				BaseResponseEntity<FindResultBean> entity = new BaseParser<FindResultBean>().parserDomain(response, FindResultBean.class);
				if(entity != null && entity.getState()){
					FindResultBean data = entity.getSingleDomain();
					if(data != null && data.getList() != null && data.getList().size() >0){
						adapter = new FindResultAdapter(data.getList());
						listView.setAdapter(adapter);
						tv_count.setText(data.getUsercount()+"分");
					} else {
						ToastUtils.getNormalCenterToast(CTX, "无法获取信息");
					}
				}else{
					ToastUtils.getNormalCenterToast(CTX, entity.getMsg());
				}
			}
		}, CTX);
	}
	
	
	class FindResultAdapter extends BaseAdapter{
		List<FindResult> data;
		public FindResultAdapter(List<FindResult> data) {
			super();
			this.data = data;
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public FindResult getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				convertView = inflater.inflate(R.layout.find_jingcaijieguo_item, null);
				holder = new ViewHolder();
				holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				holder.tv_right_result = (TextView) convertView.findViewById(R.id.tv_right_result);
				holder.tv_zongji = (TextView) convertView.findViewById(R.id.tv_zongji);
				holder.tv_useranser_count = (TextView) convertView.findViewById(R.id.tv_useranser_count);
				holder.tv_deadline = (TextView) convertView.findViewById(R.id.tv_deadline);
				holder.ll_timus = (LinearLayoutForListView) convertView.findViewById(R.id.ll_timus);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			FindResult item = getItem(position);
			holder.tv_title.setText(item.getTitle());
			holder.tv_right_result.setText(item.getText());
			holder.tv_useranser_count.setText(String.format(getResources().getString(R.string.user_answer_count), item.getAnswercount()));
			holder.tv_zongji.setText(String.format(getResources().getString(R.string.user_zongji), item.getScores(),item.getAverageodds()));
			holder.tv_deadline.setText(item.getThetime());
			holder.ll_timus.setAdapter(new TimuAdapter(item.getHistory()));
			return convertView;
		}
		
		
		class ViewHolder{
			TextView tv_title,tv_right_result,tv_useranser_count,tv_zongji,tv_deadline;
			LinearLayoutForListView ll_timus;
		}
	}
	
	class TimuAdapter extends BaseAdapter{

		List<Answers> history;
		public TimuAdapter(List<Answers> history) {
			this.history = history;
		}

		@Override
		public int getCount() {
			return history.size();
		}

		@Override
		public Answers getItem(int position) {
			return history.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AnswerViewHolder holder = null;
			if(convertView == null){
				convertView = inflater.inflate(R.layout.find_jingcai_answer_item, null);
				holder = new AnswerViewHolder();
				holder.tv_time_youranswer = (TextView) convertView.findViewById(R.id.tv_time_youranswer);
				holder.tv_jifen_jiangli = (TextView) convertView.findViewById(R.id.tv_jifen_jiangli);
				holder.tv_jiangli = (TextView) convertView.findViewById(R.id.tv_jiangli);
				convertView.setTag(holder);
			}else{
				holder = (AnswerViewHolder) convertView.getTag();
			}
			Answers item = getItem(position);
			holder.tv_time_youranswer.setText(String.format(getResources().getString(R.string.time_answer), item.getAnswertime(),item.getAnswersequence()));
			holder.tv_time_youranswer.setText(String.format(getResources().getString(R.string.jifen_jiangli), item.getQuizintegral(),item.getPlayingodds()));
			holder.tv_jifen_jiangli.setText(String.format(getResources().getString(R.string.jingcai_jiangli), item.getQuizscores()));
			return convertView;
		}
		
		class AnswerViewHolder{
			TextView tv_time_youranswer,tv_jifen_jiangli,tv_jiangli;
		}
	}
	
	
}
